"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFLybzjCtrl
 * @description
 * # FinanceFLybzjCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "FinanceFLybzjCtrl",
    function ($scope, $rootScope, openApi, Util, $location, $routeParams, localStorageService, FormSubmit) {
      var id = ($scope.process_id = $routeParams.id);
      $scope.payTypeList = [
        {
          key: "汇履约保证金",
          value: 2
        },
        {
          key: "退履约保证金",
          value: 4
        }
      ];
      $scope.projectsource = [ {
          key: "工程合同",
          value: "Project_ContractApplySingle"
        },
        {
          key: "投标申请单",
          value: "Project_BidApply"
        },
        {
          key: "项目信息",
          value: "Xm_ProjectStore"
        },
      ];
      $scope.lastReturnBackType = [
        {
          key: "未退清",
          value: 0
        },
        {
          key: "已退清",
          value: 1
        }
      ];
      $scope.payWayList = [
        {
          key: "银行保函",
          value: 1
        },
        {
          key: "柜台转账",
          value: 2
        },
        {
          key: "网银转账",
          value: 3
        },
        {
          key: "银行汇票",
          value: 4
        },
        {
          key: "现金支付",
          value: 5
        }
      ];
      $scope.resetform = function () {
        $scope.param.returnNo = "";
        $scope.param.fundSorucePerson = "";
        $scope.param.fundSoruceAccount = "";
        $scope.param.fundSoruceBank = "";
        $scope.param.fundSoruceMoney = "";
        $scope.param.receiveMoney = "";
        $scope.param.receivePerson = "";
        $scope.param.receiveAccount = "";
        $scope.param.receiveBank = "";
        $scope.param.fundSoruceList = [];
        $scope.param.receiveList = [];
        if (typeof $scope.param.lastReturnBack === "undefined") {
          $scope.param.lastReturnBack = $scope.param.lastReturnBack || 1
        }
      };
      $scope.choosePeople = function () {
        if ($scope.param.payType == 4) {
          return;
        }
        var relModuleObj = {
          name: "FinanceFBzjCtrl",
          selectTab: "project_money_select",
          selectTabContent: "工程名称"
        };
        // 将列表的数据序列化存入缓存以便回来时候使用
        $scope.param.fundSoruceDetail = JSON.stringify(
          $scope.param.fundSoruceList
        );
        $scope.param.receiveDetail = JSON.stringify($scope.param.receiveList);
        Util.savaParamForReview($scope.param, id);
        if ($scope.param.formKey === "Xm_ProjectStore") {
          $location.path("project/chooseproject");
        } else {
          Util.savaDataforsearch(relModuleObj);
          $location.path("finance/choosepeople")
        }
      };
      $scope.disabled = function () {
        var checked = true;
        if (!$scope.param) {
          return;
        }
        if ($scope.param.payType == 4) {
          checked = false;
        }
        return checked;
      };
      $scope.chooseNo = function (ifchoose) {
        // 如果选择了退投标保证金则选择跳入页面选择编号
        if ($scope.disabled()) {
          return;
        }
        var relModuleObj = {
          name: "FinanceFBzjCtrl",
          selectTab: "remit_performance_bond",
          selectTabContent: "保证金申请单"
        };
        // 将列表的数据序列化存入缓存以便回来时候使用
        $scope.param.fundSoruceDetail = JSON.stringify(
          $scope.param.fundSoruceList
        );
        $scope.param.receiveDetail = JSON.stringify($scope.param.receiveList);
        Util.savaParamForReview($scope.param, id);
        Util.savaDataforsearch(relModuleObj);

        $location.path("finance/choosepeople");
      };
      $scope.addfundSorucePenson = function () {
        $rootScope.$emit("showmasker");
        $scope.showFundPerson = true;
      };
      $scope.deletedItem = function (index) {
        $scope.param.fundSoruceList.splice(index, 1);
        $scope.index = -1;
      };
      $scope.confirmFundPerson = function () {
        $scope.picker = $scope.picker || {};
        var check = false;
        check = checkPicker($scope.picker);
        if (!check) {
          return;
        }
        $scope.param.fundSoruceList.push($scope.picker);
        // 隐藏表单
        hideFundPerson();
      };
      var hideFundPerson = function () {
        $scope.picker = {};
        $rootScope.$emit("closemasker");
        $scope.showFundPerson = false;
      };
      $scope.$on("closemasker", function () {
        hideFundPerson();
      });
      $scope.cacelFundPerson = function () {
        hideFundPerson();
      };
      $scope.swipe = function (index) {
        $scope.index = index;
      };
      $scope.resetlist = function (index) {
        $scope.index = -1;
      };
      // 验证是否输入完整
      var checkPicker = function (data) {
        var check = true;
        if (!data.money) {
          Util.Toast("请输入付款金额");
          check = false;
          return check;
        }
        return check;
      };
      $scope.submit = function (operationId) {
        if ($scope.param.payType == 2) {
          $scope.param.fundSoruceDetail = angular.toJson(
            $scope.param.fundSoruceList
          );
        } else if ($scope.param.payType == 4) {
          $scope.param.receiveDetail = angular.toJson($scope.param.receiveList);
        }
        // 底部类型end
        var options = {};
        // 流程id
        options.id = id;
        // 根据operationId判断是草稿还是提交
        options.operationId = operationId;
        options.urlParam = openApi.performanceBond;
        options.param = angular.copy($scope.param, {});
        options.param["contractNo"] = options.param["contractNo"] || "";
        delete options.param.fundSoruceList;
        delete options.param.receiveList;
        delete options.param.formBidApply;
        options.param.amount = 1;
        FormSubmit.submit(options, $scope);
      };
      $scope.validate = function () {
        if (!$scope.param.projectName) {
          Util.Toast("请填写工程名称");
          return;
        }
        if (!$scope.param.payType) {
          Util.Toast("请选择保证金类型");
          return;
        }
        if (!$scope.disabled() && !$scope.param.returnNo) {
          Util.Toast("请选择汇保证金时的编号");
          return;
        }
        if (!$scope.param.payWay) {
          Util.Toast("请选择付款方式");
          return;
        }
        if (!$scope.param.fundSoruceMoney) {
          if ($scope.param.fundSoruceMoney !== 0) {
            Util.Toast("请填写付款金额");
            return;
          }
        }
        if ($scope.param.payType == 4) {
          if (!$scope.param.receiveMoney) {
            if ($scope.param.receiveMoney !== 0) {
              Util.Toast("请填写收款金额");
              return;
            }
          }
        }
        if (!$scope.param.endDate) {
          Util.Toast("请选择到账截至日期");
          return;
        }
        if (!$scope.param.remark) {
          Util.Toast("请填写备注");
          return;
        }
        if (!$scope.validation.passvalidation) {
          Util.Toast($scope.validation.tip);
          return;
        }
        return true;
      };
      var init = function () {
        // 获取回显的详细信息
        Util.getDetailData(id, function (data) {
          $scope.param = data;
          $scope.param.formKey = $scope.param.formKey || $scope.projectsource[0].value;
          var data = localStorageService.get("projectinfo");
          localStorageService.remove("projectinfo");
          $scope.param = Object.assign({}, $scope.param, data);
          // 根据后台返回的字符串创建列表，后台没有返回的话就初始化一个列表
          $scope.param.fundSoruceList = ($scope.param.fundSoruceDetail &&
            JSON.parse($scope.param.fundSoruceDetail)) || [];
          $scope.param.receiveList = ($scope.param.receiveDetail &&
            JSON.parse($scope.param.receiveDetail)) || [];
          $scope.disableInput = !!($scope.param && $scope.param.disableInput);
          if (typeof $scope.param.lastReturnBack === "undefined") {
            $scope.param.lastReturnBack = $scope.param.lastReturnBack || 1
          }

        });
      };
      // 初始化
      init();
    }
  );
